package com.example.ten.image;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.ten.R;
import com.example.ten.Utils.ContentUtils;
import com.example.ten.Utils.LoadNetDataAsyncTask;
import com.example.ten.base.BaseFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ImageFragment extends BaseFragment {
    ViewPager imgVp;
    List<ImageBean.ResultBean> imglist;
    List<Fragment>fragmentList;
    private ImageFragmentPagerAdapter adapter;

    public ImageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        fragmentList=new ArrayList<>();
        View view=inflater.inflate(R.layout.fragment_style, container, false);
        imgVp=view.findViewById(R.id.frag_vp);
        //获取网络数据
        loadData(getContext(),ContentUtils.PICTURE_URL,
                ContentUtils.FILE_IMAGE,false,true,false);
        return view;
    }

    @Override
    public void showDataFromNet(String s) {
        ImageBean imageBean = new Gson().fromJson(s, ImageBean.class);
        imglist = imageBean.getResult();
        for (int i = 0; i < imglist.size(); i++) {
            ItemImageFragment itemImgFrag = new ItemImageFragment();
            ImageBean.ResultBean bean = imglist.get(i);
            Bundle bundle = new Bundle();
            bundle.putSerializable("image",bean);
            itemImgFrag.setArguments(bundle);
            fragmentList.add(itemImgFrag);
        }
        //适配器
        adapter = new ImageFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        imgVp.setAdapter(adapter);
    }

}
